﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.warehouse
{
    public partial class stockOrder : BaseAdmin
    {
        readonly OrderAccount theOrderAccount = new OrderAccount();
        readonly DALCardType theCardType = new DALCardType();
        readonly DALGoods theGoods = new DALGoods();
        readonly DALCard theCard = new DALCard();
        readonly DALStockOrder theStockOrder = new DALStockOrder();
        readonly DALStockOrderGoods theStockOrderGoods = new DALStockOrderGoods();
        readonly dalCompany theCompany = new dalCompany();

        tbStockOrder oStockOrder;
        tbCompany oCompany;



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                base.ValidateLoginRule(Gift.Model.ENUM.ERule.StockOrderAdd);

                if (Request.QueryString["action"] == "del")
                {

                    this.theOrderAccount.DeleOrderAccount(Request.QueryString["code"], int.Parse(Request.QueryString["id"]));
                    UserCardCodeList.RemoveAll(item => item.UserCardId == Request.QueryString["id"]);
                    ExtAspNet.Alert.Show("删除成功！");

                }
                this.BindSupplyCompanyList();
                this.BuildForm();
                this.BindAddGoodsGrid();
            }

        }

        void BuildForm()
        {
             if (Session["stockOrderCode"] != null)
                {
                    this.txtStockOrderCode.Text = Session["stockOrderCode"].ToString();
                }

            //if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            //{
            //    oStockOrder = theStockOrder.GetModel(int.Parse(Request.QueryString["id"]));
            //    this.txtStockOrderCode.Text = oStockOrder.stockOrderCode.Trim();
            //    Session["stockOrderCode"] = this.txtStockOrderCode.Text;
            //    this.txtAddress.Text = oStockOrder.address;
            //    this.txtLinker.Text = oStockOrder.linker;
            //    this.txtLinkerPhone.Text = oStockOrder.linkerPhone;
            //    this.txtRemark.Text = oStockOrder.remark;
            //    if (this.ddlCompanyList.Items.FindByValue(oStockOrder.companyId.ToString()) != null)
            //    {
            //        this.ddlCompanyList.SelectedValue = oStockOrder.companyId.ToString();
            //    }
                
                
            //}
            //else
         //   {
                this.txtStockOrderCode.Text = DateTime.Now.ToString("yyyyMMddhhmmssffff");
                Session["stockOrderCode"] = this.txtStockOrderCode.Text;
          //  }

        }

        void BindSupplyCompanyList()
        {
            string sWhere = "";
            if (string.IsNullOrEmpty( base.CompanyId))//系统采购
            {
                sWhere = "  id != 0 ";
            }
            else
            {
                this.ddlCompanyList.Hidden = true;
            }
            DataTable table = theCompany.GetList(sWhere).Tables[0];
            this.ddlCompanyList.DataTextField = "name";
            this.ddlCompanyList.DataValueField = "id";
            this.ddlCompanyList.DataSource = table;
            this.ddlCompanyList.DataBind();

                this.ddlCompanyList.Label = "选择供应商";
                this.ddlCompanyList.Items.Insert(0, new ExtAspNet.ListItem("选择供应商", "-1"));

                if (string.IsNullOrEmpty(base.CompanyId))//系统采购
                {
                    this.ddlCompanyList.SelectedValue = "-1";
                }
                else
                {
                    this.ddlCompanyList.SelectedValue = base.CompanyId;
                }
 
         
  
        }

        private void BindAddGoodsGrid()
        {
            DataTable table = theStockOrderGoods.GetListByStockOrderCode(this.txtStockOrderCode.Text.Trim()).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
        }



        protected void btnSubmitOrder_Click(object sender, EventArgs e)
        {
            //if (string.IsNullOrEmpty(Request.QueryString["id"]))
            //{
                oStockOrder = new tbStockOrder();
                oStockOrder.address = this.txtAddress.Text.Trim();
             
                oStockOrder.creater = int.Parse(base.AdminUserId);
                oStockOrder.createTime = DateTime.Now;
                oStockOrder.goodsToDate = DateTime.Parse(this.dpkSendDate.Text);
                oStockOrder.linker = this.txtLinker.Text;
                oStockOrder.linkerPhone = this.txtLinkerPhone.Text;
                oStockOrder.remark = this.txtRemark.Text.Trim();
                oStockOrder.stockStates = 0;

                if (string.IsNullOrEmpty(base.CompanyId)) //
                {
                    oStockOrder.companyId = int.Parse(this.ddlCompanyList.SelectedItem.Value);
                    oStockOrder.stockCompanyId = 0;
                }
                else
                {
                    oStockOrder.companyId = 0;
                    oStockOrder.stockCompanyId = int.Parse(base.CompanyId);
                }
                oStockOrder.stockOrderCode = this.txtStockOrderCode.Text.Trim();
                theStockOrder.Add(oStockOrder);
            //}
            //else
            //{
            //    oStockOrder = theStockOrder.GetModel(int.Parse(Request.QueryString["id"]));
            //    oStockOrder.address = this.txtAddress.Text.Trim();
            //    oStockOrder.companyId = int.Parse(this.ddlCompanyList.SelectedItem.Value);
      
            //    oStockOrder.goodsToDate = DateTime.Parse(this.dpkSendDate.Text);
            //    oStockOrder.linker = this.txtLinker.Text;
            //    oStockOrder.linkerPhone = this.txtLinkerPhone.Text;
            //    oStockOrder.remark = this.txtRemark.Text.Trim();
            //    oStockOrder.stockStates = 0;
            //    oStockOrder.stockCompanyId = 0;
            //    oStockOrder.stockOrderCode = this.txtStockOrderCode.Text.Trim();
            //    theStockOrder.Add(oStockOrder);
            //}
            Session["stockOrderCode"] = null;
            ExtAspNet.Alert.Show("提交订单成功！请记住订单编号(" + this.txtStockOrderCode.Text.Trim() + ")以便查询！", "", "window.location.href='stockOrderList.aspx'");

        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theStockOrderGoods.GetListByStockOrderCode(this.txtStockOrderCode.Text.Trim()).Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

     


        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindAddGoodsGrid();
            // ExtAspNet.Alert.Show("postback回发窗口！");
        }

        protected void Window2_Close(object sender, EventArgs e)
        {
            this.BindAddGoodsGrid();
            // ExtAspNet.Alert.Show("postback回发窗口！");
        }

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "deleted")
            {
                theStockOrderGoods.Delete(int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString()));

                ExtAspNet.Alert.Show("商品删除成功！");
                this.BindAddGoodsGrid();
            }
        }

        protected void ddlCompanyList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlCompanyList.SelectedItem.Value != "-1")
            {
                oCompany = theCompany.GetModel(int.Parse(this.ddlCompanyList.SelectedItem.Value));
                this.txtAddress.Text = oCompany.Provence + "," + oCompany.City + "," + oCompany.Address;
                this.txtLinker.Text = oCompany.Linker;
                this.txtLinkerPhone.Text = oCompany.Mobile;
            }
        }



    }
}